<template>
  <div class="app-page">
    <el-table-form ref="form" :model="query">
      <el-form-item class="merge-item">
        <el-select v-model="conditionType" style="width: 170px; margin-right: 2px;">
          <el-option label="单号" value="otherCode" />
          <el-option label="客户电话" value="contactPhone" />
          <el-option label="客户编号" value="csmCode" />
          <el-option label="客户名称" value="csmName" />
          <el-option label="处理人" value="userName" />
          <el-option label="登记人" value="answer" />
        </el-select>
        <el-input v-model.trim="csmText" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item :table-form-merge="2" label="登记时间:">
        <el-date-picker
          v-model="date"
          type="datetimerange"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          format="yyyy-MM-dd HH:mm"
          :clearable="false"
          :default-time="['00:00:00', '23:59:59']"
        />
      </el-form-item>
      <el-form-item label="责任机构:" prop="orgSNNo">
        <el-tree-select
          v-model="query.orgSNNo"
          :clearable="true"
          class="tree-select"
          :popover-width="360"
          :select-params="{
            clearable: true,
            placeholder: '请选择',
            loading: $loadingPlugin.getTreeData
          }"
          :tree-params=" {
            'expand-on-click-node': false,
            'highlight-current': true,
            'filterable': true,
            props:{
              value:'orgSNNo',
              label: 'orgName',
              disabled: 'disabled',
            },
            data: treeData
          }"
        />
      </el-form-item>
      <template v-slot:btns>
        <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
      </template>
    </el-table-form>
    <lb-table
      ref="lbTable"
      v-loading="$loadingPlugin.fetchData"
      v-el-height-adaptive-table="{bottomOffset:10}"
      border
      highlight-current-row
      element-loading-text="加载中"
      :column="tableData.column"
      :data="tableData.data"
      pagination
      :total="tableData.total"
      custom-column
      @fetch="fetchData"
    />
    <remark-order ref="remarkOrder" />
  </div>
</template>

<script>
  import { queryOtherWorkOrderList, queryOwnersList, queryCsmTypeMap, queryBackBottleStatusMap, queryVisitStatusMap, queryCallCheckMap } from '@/api'
  import { parseTime } from '@/utils'
  import getTreeData from '@/utils/jsonTree'
  import { mapGetters } from 'vuex'
  import moment from 'dayjs'
  import RemarkOrder from './components/Remark'

  export default {
    name: 'otherWorkOrder',
    components: {
      RemarkOrder
    },
    data() {
      return {
        treeData: [],
        conditionType: 'contactPhone',
        date: [moment().startOf('day'), moment().endOf('day')],
        csmText: '',
        query: {
          orgSNNo: '' // 所属机构orgSNNo
        },
        roleTypeList: [],
        tableData: {
          total: 0,
          column: [
            {
              type: 'index'
            },
            {
              prop: 'otherCode',
              width: 150,
              label: '单号'
            },
            {
              prop: 'userName',
              label: '处理人'
            },
            {
              prop: 'contactAddress',
              label: '联系地址',
              showOverflowTooltip: true,
              width: 150
            },
            {
              prop: 'contactPhone',
              label: '联系电话',
              width: 110
            },
            {
              prop: 'orgName',
              label: '责任机构',
              width: 110
            },
            {
              prop: 'csmCode',
              label: '客户编号'
            },
            {
              prop: 'csmName',
              label: '客户名称'
            },
            {
              prop: 'contact',
              label: '联系人'
            },
            {
              prop: 'answer',
              label: '登记人'
            },
            {
              prop: 'createTime',
              label: '登记时间',
              width: 160,
              render: (h, { row }) => <span>{ row.createTime && parseTime(row.createTime, '{y}-{m}-{d} {h}:{i}') }</span>
            },
            {
              prop: 'description',
              width: 160,
              showOverflowTooltip: true,
              label: '描述'
            },
            {
              prop: 'remark',
              showOverflowTooltip: true,
              label: '备注',
              width: 110
            },
            {
              label: '操作',
              align: 'center',
              fixed: 'right',
              width: 80,
              render: (h, { row, $index }) => {
                return (
                  <div>
                    <el-link underline={false} size='mini' onClick={() => this.openRemark(row, $index)}>
                      处理备注
                    </el-link>
                  </div>
                )
              }
            }
          ],
          data: []
        }
      }
    },
    computed: {
      ...mapGetters([
        'orgList'
      ])
    },
    async created() {
      await Promise.all([this.getTreeData()])
    },
    methods: {
      // 获取所属机构数据
      async getTreeData() {
        const data = this.orgList.map(item => ({
          label: item.name,
          ...item
        }))
        this.treeData = getTreeData(data)
      },
      _getQuery(obj) {
        const query = {}
        for (const key in obj) {
          if (obj.hasOwnProperty(key)) {
            if (obj[key] !== '') {
              query[key] = obj[key]
            }
          }
        }
        return query
      },
      async fetchData({ currentPage, pageSize }) {
        const query = {
          ...this._getQuery(this.query),
          currentPage,
          pageSize
        }
        if (this.date && this.date.length > 0) {
          query.createTime = moment(this.date[0]).format()
          query.createTimeEnd = moment(this.date[1]).format()
        }
        query[this.conditionType] = this.csmText
        const diff = moment(this.date[1]).diff(moment(this.date[0]), 'days')
        if (diff > 366) {
          this.$message.error('登记时间最大支持查询1年')
          return
        }
        const res = await queryOtherWorkOrderList(query)
        this.tableData.data = res.data.rows
        if (currentPage === 1) this.tableData.total = res.data.total
      },
      // 查询处理人列表
      async getOwnersList(org) {
        // const b = corpsetting.sp1 >> 2 & 0x01
        const res = await queryOwnersList({
          orgId: org ? org.orgId : -1
          // corpparm1: b
        })
        this.ownersList = res.data
      },
      // 查询客户类型枚举
      async getCsmTypeMap() {
        const res = await queryCsmTypeMap({})
        this.csmTypeMap = res.data
      },
      // 查询状态枚举
      async getBackBottleStatusMap() {
        const res = await queryBackBottleStatusMap({})
        this.rstatusMap = res.data
      },
      // 查询回访结果枚举
      async getVisitStatusMap() {
        const res = await queryVisitStatusMap({})
        this.visitStatusMap = res.data
      },
      // 查询回访调查枚举
      async getCallCheckMap() {
        const res = await queryCallCheckMap({})
        this.callCheckMap = res.data
      },
      async openDetail(row) {
        await this.$refs.detail.open(row)
      },
      // 点击芯片编码/气瓶编码
      async openBottleDetail(row) {
        await this.$refs.bottleDetail.open(row)
      },
      // 点击催单
      async openPressOrder(row) {
        await this.$refs.pressOrder.open(row)
        this.$refs.lbTable.fetch({ currentPage: 1 })
      },
      // 点击退单
      async openBackOrder(row) {
        await this.$refs.backOrder.open(row)
        this.$refs.lbTable.fetch({ currentPage: 1 })
      },
      // 点击转派
      async openTransferOrder(row) {
        await this.$refs.transferOrder.open(row)
        this.$refs.lbTable.fetch({ currentPage: 1 })
      },
      // 点击备注
      async openRemark(row) {
        await this.$refs.remarkOrder.open(row)
        this.$refs.lbTable.fetch({ currentPage: 1 })
      },
      exportFile() {},
      print() {},
      search() {
        this.$refs.lbTable.fetch({ currentPage: 1 })
      },
      // 模式选择
      modeChange(value) {
        if (value === 2) {
          this.tableData.column.unshift({
            type: 'selection',
            width: 40
          })
        } else {
          this.tableData.column.shift()
        }
      },
      reset() {
        this.$refs.form.resetFields()
      }
    }
  }
</script>

<style lang="scss" scoped>
.el-form-item .el-form-item {
  margin-bottom: 0;
}
.merge-item {
  /deep/ .el-form-item__content {
    display: flex;
  }
}
.btn-group {
  display: flex;
  justify-content: space-between;
}
</style>
